<template>
  <!-- 周转卡表表单弹窗 -->
  <el-dialog
    v-model="visibleDialog"
    :title="props.title"
    width="75%"
    draggable
    @close="closeDialog"
    :append-to-body="true"
  >
    <div class="app-container">
      <!-- <div class="search-container">
        <el-form :model="searchParam" :inline="true">
          <el-form-item label="日期">
            <el-date-picker
              v-model="searchParam.smeltDateSearch"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              value-format="YYYY-MM-DD HH:mm:ss"
              @change="getsmeltDate"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="search"
              ><i-ep-search />搜索</el-button
            >
            <el-button @click="reset"><i-ep-refresh />重置</el-button>
          </el-form-item>
        </el-form>
      </div> -->

      <!-- <el-card shadow="never" class="table-container"> -->
      <!-- <template #header>
          <el-button type="success" plain @click="openDialog()"
            ><i-ep-plus />新增</el-button
          >
          <el-button
            type="danger"
            plain
            :disabled="ids.length === 0"
            @click="handleTableDelete()"
            ><i-ep-delete />删除</el-button
          >
        </template> -->

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
        :header-cell-style="{ textAlign: 'center' }"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column
          type="index"
          label="序号"
          width="70"
          key="id"
          align="center"
        />
        <el-table-column
          label="产品名称"
          prop="materialName"
          min-width="180"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{ scope.row.materialName }}
          </template>
        </el-table-column>
        <el-table-column
          label="生产日期"
          prop="planActualDate"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="生产批次号"
          prop="plannedBatch"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="计划人员"
          prop="planUsers"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="炉号"
          prop="stoveNum"
          min-width="100"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="铁水重量"
          prop="waterWeight"
          min-width="100"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="浇注区间时间"
          prop="sectionTime"
          min-width="110"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="电炉残汤"
          prop="electricStoveResidual"
          min-width="100"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="浇注模数"
          prop="mouldQuantity"
          min-width="100"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="每一炉熔化时间分钟"
          prop="meltingTime"
          min-width="100"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="浇注温度"
          prop="temperature"
          min-width="100"
          show-overflow-tooltip
          align="right"
        />
        <el-table-column
          label="实际总铁水量(出铁量+电炉残汤)"
          prop="actualTotalQuantity"
          min-width="140"
          show-overflow-tooltip
          align="right"
        />
        <!--<el-table-column
          label="工时（每一炉熔化时间）"
          prop="manHour"
          min-width="100"
          show-overflow-tooltip
          align="right"
        />-->
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column fixed="right" label="操作" width="180" align="center">
          <template #default="scope">
            <!--<el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>-->
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      /> -->
      <!-- </el-card> -->

      <mespouringplanreport-edit
        :visible="addDialog.visible"
        :title="addDialog.title"
        :data="addDialog.data"
        @reset-table="getList"
        :is-readonly="addDialog.readvisible"
        @change-visible="changeVisible"
      />
    </div>

    <!-- <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template> -->
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary";

const dictionaryStore = useDictStoreHook();
import { useTable, useDictionary } from "@/utils/useTable";
defineOptions({
  name: "DAILYRECORDFILLINGDIALOG",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  id: {
    type: String,
    default: () => {
      return "";
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    getList();
  }
);

const rules = reactive({});

const formRef = ref(ElForm);

let formData = reactive({
  data: [],
});

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
//时间格式处理，yyyy-mm-dd

// 请求参数
const requestApi = "/mes/mespouringplanreport/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  smeltDateSearch: [],
  smeltDateStart: "",
  smeltDateEnd: "",
};

const deleteParam = {
  api: "/mes/messmeltdailyrecord/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  // getTableList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
async function getList() {
  request({
    url: `/mes/mespouringplanreport/getList`,
    method: "post",
    data: {
      planId: props.data.id,
    },
  }).then((data) => {
    data.data.forEach((item: any) => {
      item.materialName = props.data.materialName;
      item.planUsers = props.data.planUsers;
      item.plannedBatch = props.data.plannedBatch;
      item.planActualDate = props.data.planActualDate.split(" ")[0];
    });
    tableData.value = data.data;
  });
}
// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mespouringplanreport/get/` + id,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开熔炼日记录表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改浇注日记录";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增浇注日记录";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开熔炼日记录表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "熔炼日记录详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

function getsmeltDate(data: any) {
  if (data) {
    searchParam.value.smeltDateStart = data[0];
    searchParam.value.smeltDateEnd = data[1];
  } else {
    searchParam.value.smeltDateStart = "";
    searchParam.value.smeltDateEnd = "";
  }
}

async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/mes/mespouringplanreport/deleteItem/${id}`,
      method: "post",
    });
    getList();
    ElMessage.success("删除成功");
  });
}
</script>
